package com.aaa.doctor.service.impl;

import com.aaa.doctor.mapper.CareOrderItemMapper;
import com.aaa.doctor.mapper.CareOrderMapper;
import com.aaa.doctor.service.CareOrderItemService;
import com.aaa.entity.CareOrder;
import com.aaa.entity.CareOrderItem;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author MuChengGuo
 * @date 2023/4/3 10:30
 * Description: TODO
 **/
@Service
public class CareOrderItemServiceImpl implements CareOrderItemService {
    @Autowired(required = false)
    private CareOrderItemMapper careOrderItemMapper;
    @Autowired(required = false)
    private CareOrderMapper careOrderMapper;


    @Override
    public Result<String> deleteCareOrderItemById(String itemId, BigDecimal amount) {
        QueryWrapper<CareOrderItem> wrapper = new QueryWrapper<>();
        wrapper.eq("item_id", itemId);
        CareOrderItem careOrderItem = careOrderItemMapper.selectOne(wrapper);

        QueryWrapper<CareOrder> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("co_id", careOrderItem.getCoId());
        CareOrder careOrder = careOrderMapper.selectOne(wrapper1);

        careOrder.setAllAmount(careOrder.getAllAmount().subtract(amount));
        careOrderMapper.updateById(careOrder);

        if (careOrder.getAllAmount().intValue() == 0) {
            careOrderMapper.delete(wrapper1);
        }
        careOrderItemMapper.delete(wrapper);
        return new Result<>(2000, "成功", null);
    }

}
